package com.zhaoye;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @author feng
 * @Date 2021/7/7 8:59
 */

// 实现多线程下载图片,线程交替执行【同时】
public class Thread01 extends Thread {
      private String url;
      private String name;

    public Thread01(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
      Downloader loader = new Downloader();

      loader.downerloader(url,name);
        System.out.println("下载了文件名为"+name);
    }

    public static void main(String[] args) {
        Thread01 t1 = new Thread01("http://img-pre.ivsky.com/img/tupian/pre/202103/10/sunyunzhu_wuxiushan_baoshenqun-001.jpg", "1.jpg");
        Thread01 t2 = new Thread01("http://img.ivsky.com/img/tupian/t/202103/10/sunyunzhu_wuxiushan_baoshenqun-013.jpg", "13.jpg");
        Thread01 t3 = new Thread01("http://img-pre.ivsky.com/img/tupian/pre/202103/10/sunyunzhu_wuxiushan_baoshenqun-011.jpg","11.jpg");

        t1.start();
        t2.start();
        t3.start();


    }

}

// 下载器
class Downloader{

    public void downerloader(String url,String name){

        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

        }